import os
import pandas as pd
import argparse
import configparser
# from rouge_chinese import Rouge
from rouge import Rouge
from nltk.translate.bleu_score import sentence_bleu
import jieba


def run_rouge_bench_mark(row:dict,rouge:Rouge):
    hypothesis = ' '.join(jieba.cut(row['预测结果']))
    reference = ' '.join(jieba.cut(row['金标准']))
    scores = rouge.get_scores(hypothesis,reference,avg=True)
    print(scores)
    return scores['rouge-1']['f'] + scores['rouge-2']['f'] + scores['rouge-l']['f']


def run_bleu_bench_mark(row:dict):
    target_fenci =  ' '.join(jieba.cut(row['预测结果']))
    inference_fenci = ' '.join(jieba.cut(row['金标准']))
    reference = []  # 给定标准译文
    candidate = []  # 神经网络生成的句子
    # 计算BLEU
    reference.append(target_fenci.split())
    candidate = (inference_fenci.split())
    score1 = sentence_bleu(reference, candidate, weights=(1, 0, 0, 0))
    score2 = sentence_bleu(reference, candidate, weights=(0, 1, 0, 0))
    score3 = sentence_bleu(reference, candidate, weights=(0, 0, 1, 0))
    score4 = sentence_bleu(reference, candidate, weights=(0, 0, 0, 1))
    score_arr = [score1,score2,score3,score4]
    print(score_arr)
    return score_arr


def run_batch_bench_mark(data_excel_path:str)->pd.DataFrame:
    rouge = Rouge()
    data_df = pd.read_excel(data_excel_path)
    data_df['rouge_score'] = data_df.apply(lambda x:run_rouge_bench_mark(x,rouge),axis=1)
    data_df['bleu_score'] = data_df.apply(lambda x:run_bleu_bench_mark(x),axis=1)
    return data_df

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="批量测试rouge,bleu分数")
    parser.add_argument('-rep',"--raw_excel_path", type = str, help = "原始excel文件地址")
    parser.add_argument('-oep',"--out_excel_path", type = str, help = "输出excel文件地址")
    args = parser.parse_args()
    raw_excel_path = args.raw_excel_path
    out_excel_path = args.out_excel_path
    data_df = run_batch_bench_mark(raw_excel_path)
    data_df.to_excel(out_excel_path)